package servlets;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.TerminplanerConnectionPool;
import service.TerminplanerService;

@WebServlet("/DeleteTerminServlet")
public class DeleteTerminServlet extends HttpServlet{
  private static final long serialVersionUID = 1L;

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Connection con = null;
    Long terminId = null;

    try {
      terminId = Long.valueOf(request.getParameter("TerminId"));
      con = TerminplanerConnectionPool.getInstanz().getConnection();
      TerminplanerService.getInstanz().deleteTermin(con, TerminplanerService.getInstanz().getTermin(con, terminId));
      
    } catch (Exception e) {
      request.setAttribute("exception", e.getMessage());
      request.getRequestDispatcher("/WEB-INF/jsps/error.jsp").forward(request, response);
      e.printStackTrace();
      return;
    }
    finally{
      TerminplanerConnectionPool.getInstanz().saveClose(con);
    }
    
    response.sendRedirect("TerminplanerServlet");
  }
}
